// miniprogram/pages/my/history/history.js
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLastPage: [
            false, false, false, false
        ],
        defaultBG: '/images/test.jpg',
        currentIndex: 0,
        tabbarHeight: 0,
        list: [{
            id: 0,
            color: "#464c5b",
            selectedColor: "#3498DB",
            text: "浏览历史",
            iconPath: "/images/browse_record.png",
            selectedIconPath: "/images/browse_record_active.png",
            badge: ''
        },
        {
            id: 1,
            color: "#464c5b",
            selectedColor: "#3498DB",
            text: "收藏记录",
            iconPath: "/images/collection.png",
            selectedIconPath: "/images/collection_active.png",
            badge: ''
        },
        {
            id: 2,
            color: "#464c5b",
            selectedColor: "#3498DB",
            text: "评论记录",
            iconPath: "/images/comment.png",
            selectedIconPath: "/images/comment_active.png",
            badge: ''
        },
        {
            id: 3,
            color: "#464c5b",
            selectedColor: "#3498DB",
            text: "点赞记录",
            iconPath: "/images/like.png",
            selectedIconPath: "/images/like_active.png",
            badge: ''
        }],
        browseRecordDataList: [
            {
                "key": "201909291348",
                "title": "浏览历史",
                "author": "Ethan",
                "password": "",
                "cover": "",
                "description": "今天各种场合都有使用各种Linux发行版，从嵌入式设备到超级计算机，并且在服务器领域确定了地位，掌握Linux实战技巧及Linux下软件开发，可以帮助我们提高工作效率。"
            }
        ],
        collectionRecordDataList: [
            {
                "key": "201909291348",
                "title": "收藏记录",
                "author": "Ethan",
                "password": "",
                "cover": "",
                "description": "今天各种场合都有使用各种Linux发行版，从嵌入式设备到超级计算机，并且在服务器领域确定了地位，掌握Linux实战技巧及Linux下软件开发，可以帮助我们提高工作效率。"
            }
        ],
        commentRecordDataList: [

        ],
        likeRecordDataList: [

        ]
    },
    calcTabbarHeight: function () {
        let that = this;
        const query = wx.createSelectorQuery()
        query.select('#mp_tabbar').boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec(function (res) {
            //console.log(res)
            that.data.tabbarHeight = res[0].height;
            that.setData({
                tabbarHeight: that.data.tabbarHeight
            })
        })
    },
    tabChange(e) {
        let that = this;
        that.setData({
            currentIndex: e.detail.index
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        that.calcTabbarHeight();
        // 默认背景图片检查
        if (app.globalData.config) {
            that.data.defaultBG = app.globalData.config.global.default_bg;
        }

        that.setData({
            currentIndex: options.index,
            defaultBG: that.data.defaultBG
        })
        // 从本地缓存区获取数据
        // 浏览记录
        let browse_record_last_size = wx.getStorageSync('browse_record_last_size') || 0;

        let browse_record = wx.getStorageSync('browse_record') || [];
        that.setData({
            browseRecordDataList: browse_record
        })
        if (browse_record.length > 0 && browse_record.length > browse_record_last_size) {
            let list0_badge = 'list[0].badge';
            that.setData({
                [list0_badge]: 'New'
            })
            wx.setStorage({
                key: 'browse_record_last_size',
                data: browse_record.length,
            })
        } else {
            that.data.isLastPage[0] = true;
        }
        // 收藏记录
        let collection_record_last_size = wx.getStorageSync('collection_record_last_size') || 0;

        let collection_record = wx.getStorageSync('collection_record') || [];
        that.setData({
            collectionRecordDataList: collection_record
        })
        if (collection_record.length > 0 && collection_record.length > collection_record_last_size) {
            let list1_badge = 'list[1].badge';
            that.setData({
                [list1_badge]: 'New'
            })
            wx.setStorage({
                key: 'collection_record_last_size',
                data: collection_record.length,
            })
        } else {
            that.data.isLastPage[1] = true;
        }
        // 评论记录
        let comment_record_last_size = wx.getStorageSync('comment_record_last_size') || 0;

        let comment_record = wx.getStorageSync('comment_record') || [];
        that.setData({
            commentRecordDataList: comment_record
        })
        if (comment_record.length > 0 && comment_record.length > comment_record_last_size) {
            let list2_badge = 'list[2].badge';
            that.setData({
                [list2_badge]: 'New'
            })
            wx.setStorage({
                key: 'comment_record_last_size',
                data: comment_record.length,
            })
        } else {
            that.data.isLastPage[2] = true;
        }
        // 点赞记录
        let like_record_last_size = wx.getStorageSync('like_record_last_size') || 0;

        let like_record = wx.getStorageSync('like_record') || [];
        that.setData({
            likeRecordDataList: like_record
        })
        if (like_record.length > 0 && like_record.length > like_record_last_size) {
            let list3_badge = 'list[3].badge';
            that.setData({
                [list3_badge]: 'New'
            })
            wx.setStorage({
                key: 'like_record_last_size',
                data: like_record.length,
            })
        } else {
            that.data.isLastPage[3] = true;
        }

        that.setData({
            isLastPage: that.data.isLastPage
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})